jQuery(document).ready(function() {
	
	jQuery(window).on("resize", function(event){
		resizeIframe();
		jQuery('input[type="text"]').blur();
	});
	
	jQuery("input[type='text']").bind('change' , function() {
	   jQuery(this).css('background-color', 'white');
	});
	
	if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
        //alert("Browser is Safari");
		jQuery("#Province").click(function() {
		var province = jQuery("#s2id_Province .select2-chosen").text();
		if(province=="Metro Manila") 
		  jQuery("#areabrgy").show();
		else
		  jQuery("#areabrgy").hide();
		  jQuery("#s2id_Barangay").removeClass("error");
		});
	
		jQuery("select#Province").change(function () {
		var $Province = $('#Province').val();
		if ($.trim($Province).length == 0) {
			jQuery("#s2id_Province").addClass("error");	
		} else { jQuery("#s2id_Province").removeClass("error"); }
		});
		
		jQuery("select#Barangay").change(function () {
		var $Barangay = $('#Barangay').val();
		if ($.trim($Barangay).length == 0) {
			jQuery("#s2id_Barangay").addClass("error");	
		} else { jQuery("#s2id_Barangay").removeClass("error"); }
		});
		
		jQuery("select#City").change(function () {
		var $City = $('#City').val();
		if ($.trim($City).length == 0) {
			jQuery("#s2id_City").addClass("error");	
		} else { jQuery("#s2id_City").removeClass("error"); }
		});
		
		jQuery("select#Pcode").change(function() {
			var $Pcode = $('#Pcode').val();
			if ($.trim($Pcode).length == 0) {
				jQuery("#s2id_Pcode").addClass("error");	
			} else { jQuery("#s2id_Pcode").removeClass("error"); }
		});
	}
	
	/* Start of Cut Copy Paste Disable */
	
    jQuery('#Fname, #Mname, #Lname, #Nname, #Eadd, #Othercont, #datepicker, #Address').bind("cut copy paste",function(e) {
     	 e.preventDefault();
  	});
	jQuery("#datepicker").keypress(function(e) {
		e.preventDefault();
	});
	jQuery("#datepicker").keydown(function(e) {
		if (e.which === 8) {
			e.preventDefault();
            return false;
    	}
	});
	jQuery("#datepicker").attr( 'readOnly' , 'true' );
	/* End of Cut Copy Paste Disable */
  

	var current_pos = getCookie("tiangge_current_pos");
	if (current_pos > 0) {
	//alert(current_pos + "TRUE");
		if(current_pos=="1") {
		jQuery("div.wizard a:nth-of-type(1)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(2)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="2") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(3)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="3") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(4)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="4" || current_pos=="5") {
		jQuery("div.wizard a:nth-of-type(1),div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3),div.wizard a:nth-of-type(4)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(5)").attr("class","width100 current");
		
		  var mode = getCookie("tiangge_mode");
		  var promo = getCookieR("tiangge_promo");
		  var promoDesc = getCookieR("tiangge_promoDesc");
		  /* var callDesc = getCookieR("tiangge_callDesc");
		  var textDesc = getCookieR("tiangge_textDesc");
		  var surfDesc = getCookieR("tiangge_surfDesc");
		  cookiestring = "";
			if(textDesc!=="") cookiestring += textDesc;
			if(callDesc !==null) cookiestring += ", " + callDesc;
			if(surfDesc!=="") cookiestring += " and " + surfDesc; */
		  var commitmentBox = getCookieR("tiangge_commitmentBox");
		  var mobtel = getCookieR("tiangge_mobtel");
		  var username = getCookieR("tiangge_username");
		  var eadd = getCookieR("tiangge_eadd");
		  var bday = getCookieR("tiangge_bday");
          var address = getCookieR("tiangge_address");
		  var area = getCookieR("tiangge_area");
		  var town = getCookieR("tiangge_town");
		  var province = getCookieR("tiangge_province");
		  var postalcode = getCookieR("tiangge_postalcode");
		
          if(mode!="edit"){ 
			jQuery(".promoBox, .commitmentBox, .prepaidBox, .profileBox").addClass("active");
			jQuery(".pnone").hide();
			if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
				promoDesc = promoDesc.replace("john", ",");
				promoDesc = promoDesc.replace("john ", ", ");
				jQuery('#promo').html(promo + promoDesc);
			} else {
				jQuery('#promo').html(promo + promoDesc);
			}
			jQuery('#commitmentBox').text(commitmentBox);
			if(mobtel=="0"){jQuery('#mobtel').text("none");} else {jQuery('#mobtel').text(mobtel);}
			jQuery('#name').text(username);
			jQuery('#email').text(eadd);
			jQuery('#dob').text(bday);
			
			if(province=="Metro Manila") 
				var address = address + ', ' + area + ', ' + town + ', ' + province + ', ' + postalcode;
			else
				var address = address + ', ' + town + ', ' + province + ', ' + postalcode;
				
			jQuery('#add').text(address);
			jQuery("#pname, #pemail, #pdob, #padd").show();
		  }
		}
	} else {
		//alert(current_pos + "FALSE");
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2), div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
	} 
	
	jQuery("#Fname").change(function() {
		var $Fname = $('#Fname').val();
		if ($.trim($Fname ).length == 0) {
			jQuery("#Fname").addClass("error");	
		} else { jQuery("#Fname").removeClass("error"); }
	});
	
	jQuery("#Mname").change(function() {
		var $Mname = $('#Mname').val();
		if ($.trim($Mname ).length == 0) {
			jQuery("#Mname").addClass("error");	
		} else { jQuery("#Mname").removeClass("error"); }
	});
	
	jQuery("#Lname").change(function() {
		var $Lname = $('#Lname').val();
		if ($.trim($Lname ).length == 0) {
			jQuery("#Lname").addClass("error");	
		} else { jQuery("#Lname").removeClass("error"); }
	});
	
	jQuery("#Eadd").change(function() {
		var $Eadd = $('#Eadd').val();
		if ($.trim($Eadd ).length == 0) {
			jQuery("#Eadd").addClass("error");	
			validateEmail($Eadd);
		} else { jQuery("#Eadd").removeClass("error"); }
	});
	
	jQuery("#datepicker").change(function() {
		var $datepicker = $('#datepicker').val();
		if ($.trim($datepicker ).length == 0) {
			jQuery("#datepicker").addClass("error");	
		} else { jQuery("#datepicker").removeClass("error"); }
	});
	
	jQuery("#Address").change(function() {
		var $Address = $('#Address').val();
		if ($.trim($Address).length == 0) {
			jQuery("#Address").addClass("error");	
		} else { jQuery("#Address").removeClass("error"); }
	});

	jQuery("select#Pcode").change(function() {
		var $Pcode = $('#Pcode').val();
		if ($.trim($Pcode).length == 0) {
			jQuery("#Pcode").addClass("error");	
			jQuery("#s2id_Pcode a").css("border","2px solid #ff0000");	
		} else { 
			jQuery("#Pcode").removeClass("error");
			jQuery("#s2id_Pcode a").css("border","2px solid #b9babe");	
		}
	});
	
//	jQuery("#btn_next").click(function() {
//		var $Fname = $('#Fname').val();
//		if ($.trim($Fname).length == 0) {
//			jQuery("#Fname").addClass("error");	
//		} else { jQuery("#Fname").removeClass("error"); }
//
//		var $Mname = $('#Mname').val();
//		if ($.trim($Mname ).length == 0) {
//			jQuery("#Mname").addClass("error");	
//		} else { jQuery("#Mname").removeClass("error"); }
//
//		var $Lname = $('#Lname').val();
//		if ($.trim($Lname).length == 0) {
//			jQuery("#Lname").addClass("error");	
//		} else { jQuery("#Lname").removeClass("error"); }
//
//		var $Eadd = $('#Eadd').val();
//		if ($.trim($Eadd ).length == 0) {
//			jQuery("#Eadd").addClass("error");	
//		} else { validateEmail($Eadd); }
//		
//		var $Bday = $('#datepicker').val();
//		if ($.trim($Bday ).length == 0) {
//			jQuery("#datepicker").addClass("error");	
//		} else { jQuery("#datepicker").removeClass("error"); }
//
//		var $Address = $('#Address').val();
//		if ($.trim($Address).length == 0) {
//			jQuery("#Address").addClass("error");
//		} else { jQuery("#Address").removeClass("error"); }
//
//		var $Province = $('#Province').val();
//		if ($.trim($Province).length == 0) {
//			jQuery("#Province").addClass("error");	
//		} else { $("#Province").removeClass("error"); }
//		
//		var $City = $('#City').val();
//		if ($.trim($City ).length == 0) {
//			jQuery("#City ").addClass("error");	
//			jQuery("#s2id_City a").css("border","2px solid #ff0000");	
//		} else { 
//			jQuery("#City").removeClass("error"); 
//			jQuery("#s2id_City a").css("border","2px solid #b9babe");	
//		}
//
//		var $Barangay = $('#Barangay').val();
//		if (jQuery("#areabrgy").is(":visible")){
//			if ($.trim($Barangay).length == 0) {
//				jQuery("#Barangay").addClass("error");
//				jQuery("#s2id_Barangay a").css("border","2px solid #ff0000");	
//			} else { 
//				jQuery("#Barangay").removeClass("error"); 
//				jQuery("#s2id_Barangay a").css("border","2px solid #b9babe");	
//			}
//		} else { 
//			jQuery("#Barangay").removeClass("error"); 
//			jQuery("#s2id_Barangay a").css("border","2px solid #b9babe");	
//		}
//		
//		/*var $Barangay = $('#Barangay').val();
//		if ($.trim($Barangay).length == 0) {
//			var province = jQuery("#s2id_Province .select2-chosen").text();
//		    if(province=="Metro Manila") 
//			  jQuery("#Barangay").addClass("error");
//			  jQuery("#Barangay").focus();
//		} else { jQuery("#Barangay").removeClass("error"); }*/
//		
//		var $Pcode = $('#Pcode').val();
//		if ($.trim($Pcode).length == 0) {
//			jQuery("#Pcode").addClass("error");	
//			jQuery("#s2id_Pcode a").css("border","2px solid #ff0000");	
//		} else { 
//			jQuery("#Pcode").removeClass("error"); 
//			jQuery("#s2id_Pcode a").css("border","2px solid #b9babe");	
//		}
//		
//		if(navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
//			//alert("Browser is Safari");
//			var $Province = $('#Province').val();
//			if ($.trim($Province).length == 0) {
//				jQuery("#s2id_Province").addClass("error");	
//				//jQuery("#s2id_Province").focus();	
//			} else { $("#s2id_Province").removeClass("error"); }
//			
//			var $City = $('#City').val();
//			if ($.trim($City ).length == 0) {
//				jQuery("#s2id_City ").addClass("error");	
//				//jQuery("#City").focus();	
//			} else { $("#s2id_City ").removeClass("error"); }
//			
//			var $Barangay = $('#Barangay').val();
//			if (jQuery("#areabrgy").is(":visible")){
//				if ($.trim($Barangay).length == 0) 
//				jQuery("#s2id_Barangay").addClass("error");
//				//jQuery("#Barangay").focus();
//			} else { jQuery("#s2id_Barangay").removeClass("error"); }
//		
//			var $Pcode = $('#Pcode').val();
//			if (jQuery.trim($Pcode).length == 0) {
//				jQuery("#s2id_Pcode").addClass("error");	
//				//jQuery("#Pcode").focus();
//			} else { jQuery("#s2id_Pcode").removeClass("error"); }
//		}	
//		
//		if ("window", $( window ).width() < 361) {
//			if(jQuery("#datepicker, #Address, #Province, #City, #Barangay, #Pcode").hasClass('error')) {
//				scrollFrameHeight('800');
//			}	
//			if(jQuery("#Fname, #Mname, #Lname, #Eadd").hasClass('error')) {
//				scrollFrameHeight('200');
//			}		
//		} else if ("window", w( window ).width() < 641) {
//			if(w("#Pcode").hasClass('error')) {
//				scrollFrameHeight('1100');
//			}
//			if(jQuery("#Address, #Province, #City, #Barangay").hasClass('error')) {
//				scrollFrameHeight('900');
//			}
//			if(jQuery("#Eadd, #datepicker").hasClass('error')) {
//				scrollFrameHeight('650');
//			}	
//			if(jQuery("#Fname, #Mname, #Lname").hasClass('error')) {
//				scrollFrameHeight('250');
//			}	
//		} else {
//			if(w("#datepicker, #Address, #Province, #City, #Barangay, #Pcode").hasClass('error')) {
//				scrollFrameHeight('600');
//			}	
//			if(jQuery("#Fname, #Mname, #Lname, #Eadd").hasClass('error')) {
//				//scrollFrameHeight($('#Nname').height());
//				scrollFrameHeight('100');
//			}	
//		}
//		if(jQuery("#Fname").hasClass('error')) a = 1; else a = 0;
//		if(jQuery("#Mname").hasClass('error')) b = 1; else b = 0;
//		if(jQuery("#Lname").hasClass('error')) c = 1; else c = 0;
//		if(jQuery("#Eadd").hasClass('error')) d = 1; else d = 0;
//		if(jQuery("#datepicker").hasClass('error')) e = 1; else e = 0;
//		if(jQuery("#Address").hasClass('error')) f = 1; else f = 0;
//		if(jQuery("#s2id_City").hasClass('error')) g = 1; else g = 0;
//		if(jQuery("#s2id_Barangay").hasClass('error')) h = 1; else h = 0;
//		if(jQuery("#s2id_Pcode").hasClass('error')) i = 1; else i = 0;
//		if(jQuery("#s2id_Province").hasClass('error')) j = 1; else j = 0;
//		var province = jQuery("#s2id_Province .select2-chosen").text();
//		if(province=="Metro Manila") 	
//			var toterr = parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d) + parseInt(e) + parseInt(f) + parseInt(g) + parseInt(h) + parseInt(i) + parseInt(j);
//		else
//			var toterr = parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d) + parseInt(e) + parseInt(f) + parseInt(g) + parseInt(i) + parseInt(j);
//			
//		if(jQuery.isEmptyObject(jQuery("#Pcode").val())==false){
//		
//		if(toterr=='0' && jQuery("#Pcode").val()!=""){ 
//		
//		var username = jQuery("#Fname").val() + " " + jQuery("#Mname").val() + " " + jQuery("#Lname").val();
//		
//		var mName = jQuery("#Mname").val();
//		var mNameArray = []
//		var mName = mName.split(" ");
//							
//		jQuery.each(mName , function(index, value){
//            var value = value.substring(0,1);
//			mNameArray.push(value)
//        });
//							 
//		mNameArray = mNameArray.join("");	
//
//		var sumname = jQuery("#Fname").val() + " " + mNameArray + ". " + jQuery("#Lname").val();
//		var nname = jQuery("#Nname").val();
//		var othecont = jQuery("#Othercont").val();
//		var eadd = jQuery("#Eadd").val();
//		var bday = jQuery("#datepicker").val();
//		
//		if(province=="Metro Manila") {
//		    if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) 
//				var address = jQuery("#Address").val() + ' ' + jQuery("#s2id_Barangay .select2-chosen").text() + ' ' + jQuery("#s2id_City .select2-chosen").text() + ' ' + jQuery("#s2id_Province .select2-chosen").text() + ' ' + jQuery("#s2id_Pcode .select2-chosen").text();
//			else
//				var address = jQuery("#Address").val() + ', ' + jQuery("#s2id_Barangay .select2-chosen").text() + ', ' + jQuery("#s2id_City .select2-chosen").text() + ', ' + jQuery("#s2id_Province .select2-chosen").text() + ', ' + jQuery("#s2id_Pcode .select2-chosen").text();
//		} else {
//		    if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) 
//			    var address = jQuery("#Address").val() + ' ' + jQuery("#s2id_City .select2-chosen").text() + ' ' + jQuery("#s2id_Province .select2-chosen").text() + ' ' + jQuery("#s2id_Pcode .select2-chosen").text();
//			else
//				var address = jQuery("#Address").val() + ', ' + jQuery("#s2id_City .select2-chosen").text() + ', ' + jQuery("#s2id_Province .select2-chosen").text() + ', ' + jQuery("#s2id_Pcode .select2-chosen").text();
//		} 
//		
//        setCookie("tiangge_username",username);
//        setCookie("tiangge_sumname",sumname);
//        setCookie("tiangge_nname",nname);
//        setCookie("tiangge_othercont",othecont);
//        setCookie("tiangge_eadd",eadd);
//        setCookie("tiangge_bday",bday);
//        setCookie("tiangge_area",jQuery("#s2id_Barangay .select2-chosen").text());
//        setCookie("tiangge_town",jQuery("#s2id_City .select2-chosen").text());
//        setCookie("tiangge_province",jQuery("#s2id_Province .select2-chosen").text());
//        setCookie("tiangge_postalcode",jQuery("#s2id_Pcode .select2-chosen").text());
//        setCookie("tiangge_address",jQuery("#Address").val());
//		var postalIds = jQuery("#Province").val() + "|" + jQuery("#City").val() + "|" + jQuery("#Barangay").val() + "|" + jQuery("#Pcode").val();
//        setCookie("tiangge_postalIds",postalIds);
//	 
//		jQuery("#userinfo").submit();
//		    setCookie("tiangge_current_pos","4");
//		}
//		
//		}
//			
//	});
	
	jQuery("#Province").click(function(){
		var province = jQuery("#s2id_Province .select2-chosen").text();
		if(province=="Metro Manila") {
		  jQuery("#areabrgy").show();
		} else {
		  jQuery("#areabrgy").hide();
		  jQuery("#Barangay").removeClass("error");
		  jQuery("#s2id_Barangay a").css("border","2px solid #b9babe");	
		}
	});
	
	//disable spacial chars and numbers
	/* jQuery("#Fname, #Mname, #Lname, #Nname").keypress(function (evt) {
	  	 evt = evt || window.event;
  		 var charCode = evt.which || evt.keyCode;
  		 var charStr = String.fromCharCode(charCode);
  		 if ( /[a-z]/i.test(charStr) || evt.which == 8 || evt.which == 32 || evt.which == 9 || evt.which == 37 || evt.which == 39) {
			 if(jQuery(this).val().length < 1) {
				if(evt.which == 32) return false;
			 }
  		 }else{
			return false;
		 } 
	});  */
	if (navigator.userAgent.search("Firefox") >= 0) {
        jQuery("#Fname, #Mname, #Lname, #Nname").bind('keypress', function (evt) {
			evt = evt || window.event;
			 var charCode = evt.which || evt.keyCode;
			 var charStr = String.fromCharCode(charCode);
			 if (/[a-zA-Z\t.\s-]/i.test(charStr) || evt.which == 8 || evt.which == 32) {
					if(evt.which == 37) console.log("tab");
				if(jQuery(this).val().length < 1) {
					if(evt.which == 32) return false;
				 }
			 }else{
				return false;
			 } 
		});           
    } else {
		jQuery('#Fname, #Mname, #Lname, #Nname').bind('keypress', function (evt) {
			evt = evt || window.event;
			 var charCode = evt.which || evt.keyCode;
			 var charStr = String.fromCharCode(charCode);
			 if (/[a-zA-Z.-]/i.test(charStr) || evt.which == 8 || evt.which == 32) {
				 if(jQuery(this).val().length < 1) {
					if(evt.which == 32) return false;
				 }
			 }else{
				return false;
			 } 
		}); 
	}
		
	
	jQuery("#Othercont").keypress(function (evt) {
	  	 evt = evt || window.event;
  		 var charCode = evt.which || evt.keyCode;
  		 var charStr = String.fromCharCode(charCode);
  		 if ( /[a-z0-9]/i.test(charStr) || evt.which == 8 || evt.which == 9 || (evt.which > 36 || evt.which < 41)) {
  		 }else{
			return false;
		 }
		 if (evt.which != 8 && evt.which != 0 && (evt.which < 48 || evt.which > 57)) {
            return false;
    	 }
	}); 

	if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
        jQuery("#Address").bind('keypress', function (evt) {
			evt = evt || window.event;
			 var charCode = evt.which || evt.keyCode;
			 var charStr = String.fromCharCode(charCode);
			 if (/[a-zA-Z\d\t.\s-]/i.test(charStr) || evt.which == 8 || evt.which == 32) {
				if(jQuery(this).val().length < 1) {
					if(evt.which == 32) return false;
				 }
			 }else{
				return false;
			 } 
		});           
    }	
	
	if(/userprofile/.test(self.location.href)){
	   var mode = getCookie("tiangge_mode");
	   var username = getCookieR("tiangge_username");
	   var eadd = getCookieR("tiangge_eadd");
	   var bday = getCookieR("tiangge_bday");
       var address = getCookieR("tiangge_address");
	   var area = getCookieR("tiangge_area");
	   var town = getCookieR("tiangge_town");
	   var province = getCookieR("tiangge_province");
	   var postalcode = getCookieR("tiangge_postalcode");
       if(mode=="edit"){
	     jQuery(".pnone").hide();
	     jQuery(".profileBox").addClass("active");
		 jQuery('#name').text(username);
		 jQuery('#email').text(eadd);
		 jQuery('#dob').text(bday);
		 
	     if(province=="Metro Manila") 
			var address = address + ', ' + area + ', ' + town + ', ' + province + ', ' + postalcode;
		 else
			var address = address + ', ' + town + ', ' + province + ', ' + postalcode;
			
		 jQuery('#add').text(address);
		 jQuery("#pname").show();
		 jQuery("#pemail").show();
		 jQuery("#pdob").show();
		 jQuery("#padd").show();
		 jQuery('#btn_next2').text("Save");
       }
    }
	
	
});

function setCookie(cname,cvalue) {
    var d = new Date();
    d.setTime(d.getTime() + (30*60*1000));
	
	ua_tab2ics = "IMM76D";
	_ua = navigator.userAgent;
	
	if(_ua.search(ua_tab2ics) > 0)
	    document.cookie = cname+"="+cvalue+"; path=/"; 
	else{
	
	var expiration = getCookie("tiangge_expiration");
	if(expiration=="")
		var expires = "expires=" + d.toUTCString();
	else{
		var e = new Date();
		e.setTime(parseInt(expiration) + (30*60*1000));
	    var expires = "expires=" + e.toUTCString();
	}
	
	//document.cookie = cname+"="+cvalue+"; path=/;"+expires;
	document.cookie = cname+"="+cvalue+"; path=/";
	}
}

function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) 
	{
	var c = ca[i].replace(/\s/g, "");
	if (c.indexOf(name)==0) return c.substring(name.length,c.length);
	}
	return "";
}
   
function getCookieR(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


// ------------------------------
// Validate email	
// DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
// ------------------------------	
function validateEmail(email) {

		var at="@";
		var dot=".";
		var lat=email.indexOf(at);
		var lstr=email.length;
		var ldot=email.indexOf(dot);
		if (email.indexOf(at)==-1){
		   //alert("Invalid E-mail address")
		   //return false
		   jQuery("#Eadd").addClass("error");
		   jQuery("div#divEadd, #email-error").addClass("inBlock");
		   jQuery("#email-error").show();
			return "Invalid E-mail address";
		}
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").css("display","none");
		 }
		 
		if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr){
		   //alert("Invalid E-mail address")
		   //return false
		   jQuery("#Eadd").addClass("error");
		   jQuery("#email-error").show();
		   return "Invalid E-mail address";
		}
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").css("display","none");
		 }
		
		if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("div#divEadd, #email-error").addClass("inBlock");
			jQuery("#email-error").show();
			return "Invalid E-mail address";
		}
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").css("display","none");
		 }
		
		 if (email.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("div#divEadd, #email-error").addClass("inBlock");
			jQuery("#email-error").show();
			return "Invalid E-mail address";
		 }
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").css("display","none");
		 }
		 
		 if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("div#divEadd, #email-error").addClass("inBlock");
			jQuery("#email-error").show();
			return "Invalid E-mail address"; 
		 }
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").css("display","none");
		 }
		 
		 if (email.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("div#divEadd, #email-error").addClass("inBlock");
			jQuery("#email-error").show();
		    return "Invalid E-mail address";
		 }
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").css("display","none");
		 }
		
		 if (email.indexOf(" ")!=-1){
		    //alert("Invalid E-mail address")
		    //return false
			jQuery("#Eadd").addClass("error");
			jQuery("div#divEadd, #email-error").addClass("inBlock");
			jQuery("#email-error").show();
		    return "Invalid E-mail address";
		 }
		 else{
		 jQuery("#Eadd").removeClass("error");
		 jQuery("#email-error").css("display","none");
		 }
		 
 		 return true;
	
}

function isInputInvalid(inputvalue) {
	//var re = /^\w+$/;
	var re = /^[a-zA-Z ]*$/; // Letters only

	if (!re.test(inputvalue)) {
		// invalid format
		//console.log("true: " + inputvalue);
		return true;
	}
	//console.log("false: " + inputvalue);
	return false;
}


//============@win=========-=====================================

(function (w ) {
    w.fn.wUserProfile = function(options) {
        console.log("UP wUserProfile");
        // These are the defaults.
        var s = w.extend({
        }, options );
        
        //w("#userProf select").select2({minimumResultsForSearch: -1});
        w("#userProf select").select2();
        
        var methods = {
            validateEmpty:function(elem, elem2){
                
                if (w.trim(elem.val()).length === 0) {
                    if(typeof elem2 !== 'undefined'){
                        elem2.addClass("error");
                    }else{
                        elem.addClass("error");	
                    }
                } else {
                    if(typeof elem2 !== 'undefined'){
                        elem2.removeClass("error");
                    }else{
                        elem.removeClass("error");
                    }
                }
            }
        };

        w("#btn_next").click(function() {
            console.log("UP Next Click");
            var $Fname = w('#Fname');
            methods.validateEmpty($Fname);

            var $Mname = w('#Mname');
            methods.validateEmpty($Mname);

            var $Lname = w('#Lname');
            methods.validateEmpty($Lname);
            
            var $Eadd = w('#Eadd');
            if (w.trim($Eadd.val() ).length === 0) {
                $Eadd.addClass("error");	
            } else { validateEmail($Eadd.val()); }

            var $Bday = $datepicker = w('#datepicker');
            methods.validateEmpty($Bday);

            var $Address = w('#Address');
            methods.validateEmpty($Address);

            var $Province = w('#Province');
            //methods.validateEmpty($Province);
			if (w.trim($Province.val()).length == 0) {
                $Province.addClass("error");	
                w("#s2id_Province a").css("border","2px solid #ff0000");	
            } else { 
                $Province.removeClass("error"); 
                w("#s2id_Province a").css("border","2px solid #b9babe");	
            }
			
            var $City = w('#City');
            if (w.trim($City.val()).length == 0) {
                $City.addClass("error");	
                w("#s2id_City a").css("border","2px solid #ff0000");	
            } else { 
                $City.removeClass("error"); 
                w("#s2id_City a").css("border","2px solid #b9babe");	
            }

            var $Barangay = $('#Barangay');
            if (w("#areabrgy").is(":visible")){
                if (w.trim($Barangay.val()).length == 0) {
                    $Barangay.addClass("error");
                    w("#s2id_Barangay a").css("border","2px solid #ff0000");	
                } else { 
                    $Barangay.removeClass("error"); 
                    w("#s2id_Barangay a").css("border","2px solid #b9babe");	
                }
            } else { 
                $Barangay.removeClass("error"); 
                w("#s2id_Barangay a").css("border","2px solid #b9babe");	
            }

            var $Pcode = $('#Pcode');
            if (w.trim($Pcode.val()).length == 0) {
                $Pcode.addClass("error");	
                w("#s2id_Pcode a").css("border","2px solid #ff0000");	
            } else { 
                w("#Pcode").removeClass("error"); 
                w("#s2id_Pcode a").css("border","2px solid #b9babe");	
            }

            if(navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
                //alert("Browser is Safari");
                //var $Province = w('#Province');
                methods.validateEmpty($Province);
                
//                if ($.trim($Province).length === 0) {
//                    w("#s2id_Province").addClass("error");	
//                } else { w("#s2id_Province").removeClass("error"); }

                //var $City = w('#City');
                methods.validateEmpty($City, w("#s2id_City "));
//                if ($.trim($City ).length === 0) {
//                    jQuery("#s2id_City ").addClass("error");	
//                    //jQuery("#City").focus();	
//                } else { $("#s2id_City ").removeClass("error"); }

                //var $Barangay = $('#Barangay').val();
                
                if (w("#areabrgy").is(":visible")){
                    if (w.trim($Barangay.val()).length === 0) 
                        w("#s2id_Barangay").addClass("error");
                    //jQuery("#Barangay").focus();
                } else {w("#s2id_Barangay").removeClass("error"); }

                //var $Pcode = $('#Pcode').val();
                methods.validateEmpty($Pcode, w("#s2id_Pcode"));
//                if ($.trim($Pcode).length === 0) {
//                    jQuery("#s2id_Pcode").addClass("error");	
//                    //jQuery("#Pcode").focus();
//                } else { jQuery("#s2id_Pcode").removeClass("error"); }
            }	
/* 
            if ("window", w( window ).width() < 361) {
                console.log("Error < 361");
                if(w("#datepicker, #Address, #Province, #City, #Barangay, #Pcode").hasClass('error')) {
                    scrollFrameHeight('800');
                   // document.location = "#address";
                    console.log("Error #address");
                }	
                if(w("#Fname, #Mname, #Lname, #Eadd").hasClass('error')) {
                    scrollFrameHeight('300');
                   // document.location = "#Fname";
                    console.log("Error #Fname");
                }		
            } else  */
			if ("window", w( window ).width() < 641) {
                console.log("Error < 641");				
				if($Fname.hasClass('error')){
					$Fname.focus();
				} else if($Mname.hasClass('error')){
					$Mname.focus();
				} else if($Lname.hasClass('error')){
					$Lname.focus();
				} else if($Eadd.hasClass('error')){
					$Eadd.focus();
				} else if($Bday.hasClass('error')){
					$Bday.focus();
				} else if($Address.hasClass('error')){
					$Address.focus();
				} else if($Province.hasClass('error')){
					$Province.select2('open');
				} else if($City.hasClass('error')){
					$City.select2('open');
				} else if($Barangay.hasClass('error')){
					$Barangay.select2('open');
				} else if($Pcode.hasClass('error')){
					$Pcode.select2('open');
				} 
/*                 if($Pcode.hasClass('error')) {
                    scrollFrameHeight('1100');
                }
                if(w("#Address, #Province, #City, #Barangay").hasClass('error')) {
                  scrollFrameHeight('900');
               }
               if(w("#Eadd, #datepicker").hasClass('error')) {
                  scrollFrameHeight('650');
                }	
                if(w("#Fname, #Mname, #Lname").hasClass('error')) {
                    scrollFrameHeight('250');
                }	 */
            } else {
                console.log("Error NO CHOICE");					
				if(w("#datepicker, #Address, #Province, #City, #Pcode").hasClass('error')) {
                    scrollFrameHeight('700');
					//document.location = "#address"; // I this best as GOTO position than scrolling.
					//console.log("NO #address");		
                }	
                if(w("#Fname, #Mname, #Lname, #Eadd").hasClass('error')) {
                    //scrollFrameHeight($('#Nname').height());
                    scrollFrameHeight('300');
                    //document.location= "#Fname";
					//console.log("NO #Fname");		
                } 
            }
            
            if($Fname.hasClass('error')) a = 1; else a = 0;
            if($Mname.hasClass('error')) b = 1; else b = 0;
            if($Lname.hasClass('error')) c = 1; else c = 0;
            if($Eadd.hasClass('error')) d = 1; else d = 0;
            if($datepicker.hasClass('error')) e = 1; else e = 0;
            if($Address.hasClass('error')) f = 1; else f = 0;
            if(w("#s2id_City").hasClass('error')) g = 1; else g = 0;
            if(w("#s2id_Barangay").hasClass('error')) h = 1; else h = 0;
            if(w("#s2id_Pcode").hasClass('error')) i = 1; else i = 0;
            if(w("#s2id_Province").hasClass('error')) j = 1; else j = 0;
            
            var province = w("#s2id_Province .select2-chosen").text();
            
            var toterr = 0;
            if(province=="Metro Manila") 	
                toterr = parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d) + parseInt(e) + parseInt(f) + parseInt(g) + parseInt(h) + parseInt(i) + parseInt(j);
            else
                toterr = parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d) + parseInt(e) + parseInt(f) + parseInt(g) + parseInt(i) + parseInt(j);

            if(w.isEmptyObject($Pcode.val())==false){

                if(toterr=='0' && $Pcode.val()!=""){ 
                    var username = $Fname.val() + " " + $Mname.val() + " " + $Lname.val();

                    var mName = $Mname.val();
                    var mNameArray = [];
                    var mName = mName.split(" ");

                    jQuery.each(mName , function(index, value){
                        var value = value.substring(0,1);
                        mNameArray.push(value)
                    });

                    mNameArray = mNameArray.join("");	

                    var sumname = $Fname.val() + " " + mNameArray + ". " + $Lname.val();
                    var nname = w("#Nname").val();
                    var othecont = w("#Othercont").val();
                    var eadd = $Eadd.val();
                    var bday = $datepicker.val();

                    if(province === "Metro Manila") {
                        //if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) 
                            var address = $Address.val() + ' ' 
                                + w("#s2id_Barangay .select2-chosen").text() + ' '
                                + w("#s2id_City .select2-chosen").text() + ' ' 
                                + w("#s2id_Province .select2-chosen").text() + ' ' 
                                + w("#s2id_Pcode .select2-chosen").text();
                        //else
                        //    var address = $Address.val() + ', ' 
                        //        + w("#s2id_Barangay .select2-chosen").text() + ', ' 
                        //        + w("#s2id_City .select2-chosen").text() + ', ' 
                        //        + w("#s2id_Province .select2-chosen").text() + ', ' 
                        //        + w("#s2id_Pcode .select2-chosen").text();
                    } else {
                        if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) 
                            var address = $Address.val() + ' ' 
                                + w("#s2id_City .select2-chosen").text() + ' ' 
                                + w("#s2id_Province .select2-chosen").text() + ' ' 
                                + w("#s2id_Pcode .select2-chosen").text();
                        else
                            var address = $Address.val() + ', ' 
                                + w("#s2id_City .select2-chosen").text() + ', ' 
                                + w("#s2id_Province .select2-chosen").text() + ', ' 
                                + w("#s2id_Pcode .select2-chosen").text();
                    } 

                    setCookie("tiangge_username",username);
                    setCookie("tiangge_sumname",sumname);
                    setCookie("tiangge_nname",nname);
                    setCookie("tiangge_othercont",othecont);
                    setCookie("tiangge_eadd",eadd);
                    setCookie("tiangge_bday",bday);
                    setCookie("tiangge_area",w("#s2id_Barangay .select2-chosen").text());
                    setCookie("tiangge_town",w("#s2id_City .select2-chosen").text());
                    setCookie("tiangge_province",w("#s2id_Province .select2-chosen").text());
                    setCookie("tiangge_postalcode",w("#s2id_Pcode .select2-chosen").text());
                    setCookie("tiangge_address",w("#Address").val());
                    var postalIds = $Province.val() + "|" + $City.val() + "|" + $Barangay.val() + "|" + $Pcode.val();
                    setCookie("tiangge_postalIds",postalIds);

                    w("#userinfo").submit();
                    setCookie("tiangge_current_pos","4");
                }else{
                    //
                }
                return false;
            }

        }); // w("#btn_next").click(function() {
        
    }; // w.fn.wUserProfile = function(options) {
    
    w(document).ready(function() {
        console.log("UP Ready");
        var wUserProfile = w(document).wUserProfile();
    });
}( jQuery ));







